package com.ohuang.jiaxiaotong.Util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class Sys {
    public static long timeMs;

    //延时去抖  防止按钮被多次点击打开多个界面
    public static boolean isClick(){
        if (System.currentTimeMillis()-timeMs>500){
            timeMs=System.currentTimeMillis();
            return true;
        }
        return false;
    }

    public static void WriteSharedPer(Activity activity, String key, String s ) {
        SharedPreferences shpData = activity.getSharedPreferences("dataSharedPreferences",Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = shpData.edit();
        editor.putString(key, s);
        editor.apply();
    }

    public static String ReadSharedPer(Activity activity,String key,String defValue){
        SharedPreferences shar  = activity.getSharedPreferences("dataSharedPreferences",Activity.MODE_PRIVATE);
        String name = shar.getString(key,defValue);
        return name;
    }

    public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {

        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            mRecyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = mRecyclerView.getChildAt(n - firstItem).getTop();
            mRecyclerView.scrollBy(0, top);
        } else {
            mRecyclerView.scrollToPosition(n);
        }
    }


    public static void ReadRequestPower(Activity activity, Context context) {
//判断是否已经赋予权限
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {//这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限.它在用户选择"不再询问"的情况下返回false
            } else {
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,}, 1);
            }
        }

    }
}

